package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.driver.chain.chainHolder.StartServiceHolder;
import com.atguigu.daijia.driver.chain.chainHolder.StopServiceHolder;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.DriverService;
import com.atguigu.daijia.model.form.driver.DriverFaceModelForm;
import com.atguigu.daijia.model.vo.driver.DriverAuthInfoVo;
import com.atguigu.daijia.model.vo.driver.DriverLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private DriverInfoFeignClient driverInfoFeignClient;
    @Autowired
    private StartServiceHolder startServiceHolder;
    @Autowired
    private StopServiceHolder stopServiceHolder;
    /**
     * 司机端登录接口
     *
     * @param code 微信code
     * @return token值
     */
    @Override
    public String login(String code) {
        if (!Optional.ofNullable(code).isPresent()) {
            throw new GuiguException(ResultCodeEnum.ARGUMENT_VALID_ERROR);
        }
        Result<Long> result = driverInfoFeignClient.wxLogin(code);

        if (result.getCode()!=200) {
            log.error("异常信息：{}",result.getMessage());
            throw new GuiguException(ResultCodeEnum.FEIGN_FAIL);
        }
        if (!Optional.ofNullable(result.getData()).isPresent()) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        Long driverId = result.getData();
        String token = UUID.randomUUID().toString().replace("_","");
        String key = RedisConstant.USER_LOGIN_KEY_PREFIX+ token;
        stringRedisTemplate.opsForValue().
                set(key,driverId.toString(),RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.MINUTES);
        return token;
    }

    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
        Result<DriverAuthInfoVo> authInfoVoResult = driverInfoFeignClient.getDriverAuthInfo(driverId);
        DriverAuthInfoVo driverAuthInfoVo = authInfoVoResult.getData();
        return driverAuthInfoVo;
    }

    //创建司机人脸模型
    @Override
    public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        Result<Boolean> booleanResult = driverInfoFeignClient.creatDriverFaceModel(driverFaceModelForm);
        return booleanResult.getData();
    }

    @Override
    public Boolean isFaceRecognition(Long driverId) {
        return driverInfoFeignClient.isFaceRecognition(driverId).getData();
    }

    @Override
    public Boolean verifyDriverFace(DriverFaceModelForm driverFaceModelForm) {
        return driverInfoFeignClient.verifyDriverFace(driverFaceModelForm).getData();
    }

    /**
     * 采用责任链模式完成
     * @param driverId
     * @return
     */
    @Override
    public Boolean startService(Long driverId) {

        //1.判断司机是否完成认证

        //2.判断司机是否当日人脸识别

        //3.更新接单状态

        //删除Redis司机位置信息

        //删除司机缓存订单队列

       startServiceHolder.startService(driverId);

        return true;
    }

    /**
     * 停止接单服务责任链
     * @param driverId
     * @return
     */
    @Override
    public Boolean stopService(Long driverId) {
        stopServiceHolder.stopSercice(driverId);
        return true;
    }

}
